/**
 * 3维模型hooks
 */
import {
	useResizeObserver,
	THREE,
	OrbitControls,
	TransformControls,
	rendererFn,
	GLTFloaderFn,
	raycaster,
	ViewHelper,
	clock
} from '@u/threeMinx';

export class Editor {
	container = shallowRef(null); // 模型渲染的Demo节点
	renderer; //  WebGLRenderer渲染器
	scene; // 场景
	camera; // 相机
	controls; // 控制器
	transformControls; // 变换控制器
	viewHelper; // xyz坐标
	plane;
	constructor(obj) {}
	setScene() {}
	addObject(object, parent, index) {
		this.addHelper(object);
		if (parent === undefined) {
			this.scene.add(object);
		} else {
			parent.children.splice(index, 0, object);
			object.parent = parent;
		}
	}
	removeObject(object) {
		if (object.parent === null) return;
		// 材质释放内存
		object.traverse((v) => {
			if (v.type === 'Mesh') {
				v.geometry.dispose();
				v.material.dispose();
			}
		});
		// 移除
		object.parent.remove(object);
	}
	addHelper(object, helper) {
		if (helper === undefined) {
			if (object.isCamera) {
				helper = new THREE.CameraHelper(object);
			} else if (object.isPointLight) {
				helper = new THREE.PointLightHelper(object, 1);
			} else if (object.isDirectionalLight) {
				helper = new THREE.DirectionalLightHelper(object, 1);
			} else if (object.isSpotLight) {
				helper = new THREE.SpotLightHelper(object);
			} else if (object.isHemisphereLight) {
				helper = new THREE.HemisphereLightHelper(object, 1);
			} else if (object.isSkinnedMesh) {
				helper = new THREE.SkeletonHelper(object.skeleton.bones[0]);
			} else if (object.isBone === true && object.parent?.isBone !== true) {
				helper = new THREE.SkeletonHelper(object);
			} else {
				return;
			}
		}
		this.scene.add(helper);
		this.helpers[object.id] = helper;
	}
	removeHelper(object) {
		if (this.helpers[object.id] !== undefined) {
			var helper = this.helpers[object.id];
			helper.parent.remove(helper);
			delete this.helpers[object.id];
		}
	}
}
